﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daco.Web.Store;

namespace Daco.Web
{
    public enum EViewType
    {
        Tile,
        Detail
    }
}
namespace Daco.Web.Controls
{
    public partial class ProductList : Daco.Web.UI.BaseUserControl
    {
        private CTypeCollection<CProduct> _dataSource = null;

        public int Count
        {
            get
            {
                return _dataSource != null ? _dataSource.Count : 0;
            }
        }

        public int ItemsPerRow { get; set; }

        public float PriceFrom { get; set; }
        public float PriceTo { get; set; }

        private object[] _categoryFilters = null;
        public object[] CategoryFilters
        {
            get { return _categoryFilters; }
            set { _categoryFilters = value; }
        }

        private object[] _manufactureFilters = null;
        public object[] ManufactureFilters
        {
            get { return _manufactureFilters; }
            set { _manufactureFilters = value; }
        }

        private string _containName = null;
        public string ContainName
        {
            get { return _containName; }
            set { _containName = value; }
        }

        private object[] _seriesFilters = null;
        public object[] SeriesFilters
        {
            get { return _seriesFilters; }
            set { _seriesFilters = value; }
        }

        public EViewType ViewType = EViewType.Detail;
        public int PageSize { get; set; }
        public int PageIndex { get; set; }

        private bool _enablePager = true;
        public bool EnablePager
        {
            get { return _enablePager; }
            set { _enablePager = value; }
        }

        private bool _showBox = true;
        public bool ShowBox
        {
            get { return _showBox; }
            set { _showBox = value; }
        }

        private string _displayPage = "~/EStore/Catalog.aspx";
        public string DisplayPage
        {
            get { return _displayPage; }
            set { _displayPage = value; }
        }

        public ProductList()
        {
            this.PageSize = 16;
            this.PageIndex = 1;
            this.ItemsPerRow = 4;

            this.PriceFrom = int.MinValue + 1;
            this.PriceTo = int.MaxValue - 1;

            this.Init += new EventHandler(ProductList_Init);
            this.Load += new EventHandler(ProductList_Load);
        }

        void ProductList_Init(object sender, EventArgs e)
        {
            ctrlPager.DisplayPage = 7;
            ctrlPager.SelectedPage = 1;
            ctrlPager.ItemsPerPage = 16;
        }

        void ProductList_Load(object sender, EventArgs e)
        {
            _dataSource = CProduct.GetProducts(
                this.CategoryFilters,
                this.ManufactureFilters,
                this.SeriesFilters,
                this.ContainName,
                this.PriceFrom,
                this.PriceTo,
                this.PageIndex,
                this.PageSize);
            rpProduct.DataSource = _dataSource;
            rpProduct.DataBind();

            if (EnablePager && _dataSource.TotalCount > 0)
            {
                ctrlPager.Visible = true;
                ctrlPager.TotalItemCount = _dataSource.TotalCount;
            }
            else
                ctrlPager.Visible = false;

            if (_dataSource.TotalCount > 0)
                pEmpty.Visible = false;
            else
                pEmpty.Visible = true;

            ctrlPager.RelativeUrl = DisplayPage;
            ctrlPager.SelectedPage = PageIndex;
        }

        protected string GetPhotoUrl(object item)
        {
            Daco.Web.Store.CProduct p = (Daco.Web.Store.CProduct)item;
            return Page.ResolveUrl(string.Format("~/Handlers/PHOTOHandler.ashx?pid={0}", p.GetDefaulPhoto().PhotoID));
        }

        protected string GetPriceDetail(object item)
        {
            CProduct current = (CProduct)item;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (!current.EnablePriceConfidential && current.UnitPrice > 0)
            {
                sb.Append(@"<b>Giá: </b><label style=""color: Red;"">");
                sb.Append(current.UnitPrice.ToString(Config.CSiteConfig.Current.DoubleFormatString));
                sb.Append(current.Currency);
                sb.Append("</label>");
            }

            return sb.ToString();
        }
    }
}